
import random
from Database import DatabaseUtil as db

db = db('root','7k7k','wn15')
class user():
    def register(self):
        while 1:
            usn = input('请输入你的用户名')
            if self.usr_check(usn) >= 0:
                print('用户名已存在')
            else:
                break
        while 1 :
            upwd = self.upwd_check()
            upwda = input('请重新输入你的密码')
            if upwd != upwda:
                print('两次密码按输入不一致')
            else:
                break

        ubl = random.randrange(100,10000,100)
        uph = input('请输入你的联系方式')
        db.update("insert into us values('{}',{},{},{})".format(usn,upwd,ubl,uph))
        print('注册成功')
        from menu import strat_menu
        strat_menu()

    def login(self):
        uname = input('给我你的用户名')
        upwd = input('给我你的密码')
        index = self.usr_check(uname)
        if index >= 0:
            lst = db.select('select * from us')
            if lst[index][1] == upwd:
                print('登录成功')
                global user_index,usern
                usern = uname
                user_index = index
                from menu import main_menu
                main_menu()
            else:
                print('密码错误')
                from menu import strat_menu
                strat_menu()
        else:
            print('用户名不存在')
            from menu import strat_menu
            strat_menu()

    def usr_check(self,usn):
        lst = db.select('select * from us')
        for i in range(len(lst)):
            if usn == lst[i][0]:
                return i
        return -1
    def upwd_check(self):
        upwd = input('请输入一个六位以上的密码')
        if len(upwd) < 6:
            print('输入的密码太短')
            upwd = self.upwd_check()
        return upwd
    